﻿using Microsoft.Practices.Prism.ViewModel;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.Text;
using WebServerConnectorDll;

namespace FridgeManagerApp
{
    [DataContract]
    public class WUser : NotificationObject
    {
        private String openID;
        private String userName;
        private String password;
        private String nickname;
        private int    sex;
        private String province;
        private String city;
        private String country;
        private String headimgurl;
        private String unionid;

        [DataMember(Order = 0)]
        public String OpenID
        {
            get
            {
                return openID;
            }
            set
            {
                openID = value;
                RaisePropertyChanged("OpenID");
            }
        }

        [DataMember(Order = 1)]
        public String UserName
        {
            get
            {
                return userName;
            }
            set
            {
                userName = value;
                RaisePropertyChanged("UserName");
            }
        }

        [DataMember(Order = 2)]
        public String Password
        {
            get
            {
                return password;
            }
            set
            {
                password = value;
                RaisePropertyChanged("Password");
            }
        }

        [DataMember(Order = 3)]
        public String Nickname
        {
            get
            {
                return nickname;
            }
            set
            {
                nickname = value;
                RaisePropertyChanged("Nickname");
            }
        }

        [DataMember(Order = 4)]
        public int Sex
        {
            get
            {
                return sex;
            }
            set
            {
                sex = value;
                RaisePropertyChanged("Sex");
            }
        }

        [DataMember(Order = 5)]
        public String Province
        {
            get
            {
                return province;
            }
            set
            {
                province = value;
                RaisePropertyChanged("Province");
            }
        }

        [DataMember(Order = 6)]
        public String City
        {
            get
            {
                return city;
            }
            set
            {
                city = value;
                RaisePropertyChanged("City");
            }
        }

        [DataMember(Order = 7)]
        public String Country
        {
            get
            {
                return country;
            }
            set
            {
                country = value;
                RaisePropertyChanged("Country");
            }
        }

        [DataMember(Order = 8)]
        public String Headimgurl
        {
            get
            {
                return headimgurl;
            }
            set
            {
                headimgurl = value;
                RaisePropertyChanged("Headimgurl");
            }
        }

        [DataMember(Order = 9)]
        public String Unionid
        {
            get
            {
                return unionid;
            }
            set
            {
                unionid = value;
                RaisePropertyChanged("Unionid");
            }
        }

        public WUser()
        {
            OpenID = "";
            UserName = "";
            Password = "";
            Nickname = "";
            Sex = -1;
            Province = "";
            City = "";
            Country = "";
            Headimgurl = "";
            Unionid = "";
        }

        public WUser(LUser p)
        {
            OpenID = p.OpenID;
            UserName = p.UserName;
            Password = p.Password;
            Nickname = p.Nickname;
            //Sex = p.Sex;
            Province = p.Province;
            City = p.City;
            Country = p.Country;
            Headimgurl = p.Headimgurl;
            Unionid = p.Unionid;
            if (p.SexerSelected == "男")
            {
                Sex = 0;
            }
            else if (p.SexerSelected == "女")
            {
                Sex = 1;
            }
            else
            {
                Sex = 2;
            }
        }

        public static List<WUser> GetAll()
        {
            HttpResponse response = FridgeConnector.PostMessage(EStudy.User, EMethod.SelectAll, "");
            if (response.Status == "Success")
            {
                return JSON.parse<List<WUser>>(response.Data1);
            }

            return null;
        }

        public static HttpResponse Insert(List<WUser> pList)
        {
            String postData = JSON.stringify(pList);
            return FridgeConnector.PostMessage(EStudy.User, EMethod.Insert, postData);
        }

        public static HttpResponse Update(List<WUser> pList)
        {
            String postData = JSON.stringify(pList);
            return FridgeConnector.PostMessage(EStudy.User, EMethod.Update, postData);
        }

        public static HttpResponse Delete(List<WUser> pList)
        {
            String postData = JSON.stringify(pList);
            return FridgeConnector.PostMessage(EStudy.User, EMethod.Delete, postData);
        }
    }
}
